//
//  TUIVoiceMessageCell.m
//  UIKit
//
//  Created by annidyfeng on 2019/5/30.
//

#import "LGVoiceMessageCell.h"
#import "TUIDefine.h"
#import "TUIThemeManager.h"

@interface LGVoiceMessageCell()

/**
 *  语音时长标签
 *  用于在气泡外侧展示语音时长，默认数值为整数且以秒为单位。
 */
@property (nonatomic, strong)UILabel *durationLabel;

/**  接收消息 */
@property (nonatomic , strong)UIImage *incomingPic;
@property (nonatomic , strong)NSArray *incomingArray;
/**  发送消息 */
@property (nonatomic , strong)UIImage *outgoingPic;
@property (nonatomic , strong)NSArray *outgoingArray;

@end

@implementation LGVoiceMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.bubbleView addSubview:self.voiceView];
        [self.bubbleView addSubview:self.durationLabel];
        //接收端图片
        self.incomingPic = TUIChatDynamicImage(@"chat_voice_message_receiver_voice_normal_img", [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_normal")]);
        self.incomingArray = @[[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_playing_1")],[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_playing_2")],[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_receiver_playing_3")]];
        //发送端图片
        self.outgoingPic = TUIChatDynamicImage(@"message_voice_sender_playing_3", [[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_sender_playing_3")]);
        self.outgoingArray = @[[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_sender_playing_1")],[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_sender_playing_2")],[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"message_voice_sender_playing_3")]];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectMessage)];
        [self.bubbleView addGestureRecognizer:tapGesture];
    }
    return self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    NSString *sender = LGNSString(dataDic[@"sender"]);
    if ([sender isEqualToString:@"member"]) {
        //接收端
        self.durationLabel.textColor = [UIColor colorWithString:@"#333333"];
        self.voiceView.image = self.incomingPic;
        self.voiceView.animationImages = self.incomingArray;
        self.durationLabel.textAlignment = NSTextAlignmentLeft;
        [self.voiceView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bubbleView).offset(16);
            make.centerY.equalTo(self.bubbleView);
            make.width.height.equalTo(@(20));
        }];
        [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.voiceView.mas_right).offset(3);
            make.centerY.equalTo(self.voiceView);
        }];
        
    }else{
        //发送端 
        self.durationLabel.textColor = [UIColor colorWithString:@"#FFFFFF"];
        self.voiceView.image = self.outgoingPic;
        self.voiceView.animationImages = self.outgoingArray;
        self.durationLabel.textAlignment = NSTextAlignmentRight;
        [self.voiceView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bubbleView).offset(-16);
            make.centerY.equalTo(self.bubbleView);
            make.width.height.equalTo(@(20));
        }];
        [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.voiceView.mas_left).offset(-3);
            make.centerY.equalTo(self.voiceView);
        }];
    }
    
    //音频时长
    int duration = [dataDic[@"content"][@"second"] intValue];
    duration = (duration<1) ? 1:duration;
    self.durationLabel.text = [NSString stringWithFormat:@"%d\"", duration];
    
    CGFloat bubbleWidth = TVoiceMessageCell_Back_Width_Min + duration / TVoiceMessageCell_Max_Duration * Screen_Width;
    bubbleWidth = MIN(bubbleWidth, TVoiceMessageCell_Back_Width_Max);

    [self.bubbleView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(bubbleWidth));
    }];
}


-(void)onSelectMessage{
    if (self.delegate && [self.delegate respondsToSelector:@selector(playVoiceMessage:cell:)]) {
        [self.delegate playVoiceMessage:self.dataDic cell:self];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(UIImageView *)voiceView{
    if(!_voiceView){
        _voiceView = [[UIImageView alloc]init];
        _voiceView.animationDuration = 1;
    }
    return _voiceView;
}

-(UILabel *)durationLabel{
    if(!_durationLabel){
        _durationLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _durationLabel;
}

@end

